﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Expression;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;

namespace Mang24k99.Model.KEYW
{
    [ActiveRecord(Table = "KT_KEYW_NEWSTRAWORD")]
    public partial class MKtKeywNewstraword : ActiveRecordBase
    {
        private int _WordNO;

        private IList<MKtKeywWords> _MKtKeywWords;
        private IList<MKtKeywFixwords> _MKtKeywFixwords;
        private int _ID;

        private string _WordName;

        private string _ChSpell;

        private int? _IsValidate;

        private int? _IsLeaf;

        private string _PathDes;

        private int? _ParentID;

        private DateTime? _InputTime;

        private DateTime? _ModifyTime;

        private DateTime? _DeleteTime;



        [HasMany(typeof(MKtKeywWords), Table = "KT_KEYW_WORDS", ColumnKey = "MAIN_WORD")]
        public IList<MKtKeywWords> MKtKeywWords
        {
            get
            {
                return _MKtKeywWords;
            }
            set
            {
                _MKtKeywWords = value;
            }
        }
        [HasMany(typeof(MKtKeywFixwords), Table = "KT_KEYW_FIXWORDS", ColumnKey = "FIXWORD_NO")]
        public IList<MKtKeywFixwords> MKtKeywFixwords
        {
            get
            {
                return _MKtKeywFixwords;
            }
            set
            {
                _MKtKeywFixwords = value;
            }
        }

        /// <summary>
        /// Gets or sets the WordNO.()
        /// </summary>
        /// <value>The WordNO.</value>
        [PrimaryKey(PrimaryKeyType.Assigned, "WORD_NO")]
        public int WordNO
        {
            get
            {
                return _WordNO;
            }
            set
            {
                _WordNO = value;
            }
        }


        /// <summary>
        /// Gets or sets the ID.()
        /// </summary>
        /// <value>The ID.</value>
        [Property("ID")]
        public int ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }


        /// <summary>
        /// Gets or sets the WordName.()
        /// </summary>
        /// <value>The WordName.</value>
        [Property("WORD_NAME")]
        public string WordName
        {
            get
            {
                return _WordName;
            }
            set
            {
                _WordName = value;
            }
        }


        /// <summary>
        /// Gets or sets the ChSpell.()
        /// </summary>
        /// <value>The ChSpell.</value>
        [Property("CH_SPELL")]
        public string ChSpell
        {
            get
            {
                return _ChSpell;
            }
            set
            {
                _ChSpell = value;
            }
        }


        /// <summary>
        /// Gets or sets the IsValidate.()
        /// </summary>
        /// <value>The IsValidate.</value>
        [Property("IS_VALIDATE")]
        public int? IsValidate
        {
            get
            {
                return _IsValidate;
            }
            set
            {
                _IsValidate = value;
            }
        }


        /// <summary>
        /// Gets or sets the IsLeaf.()
        /// </summary>
        /// <value>The IsLeaf.</value>
        [Property("IS_LEAF")]
        public int? IsLeaf
        {
            get
            {
                return _IsLeaf;
            }
            set
            {
                _IsLeaf = value;
            }
        }


        /// <summary>
        /// Gets or sets the PathDes.()
        /// </summary>
        /// <value>The PathDes.</value>
        [Property("PATH_DES")]
        public string PathDes
        {
            get
            {
                return _PathDes;
            }
            set
            {
                _PathDes = value;
            }
        }


        /// <summary>
        /// Gets or sets the ParentID.()
        /// </summary>
        /// <value>The ParentID.</value>
        [Property("PARENT_ID")]
        public int? ParentID
        {
            get
            {
                return _ParentID;
            }
            set
            {
                _ParentID = value;
            }
        }


        /// <summary>
        /// Gets or sets the InputTime.()
        /// </summary>
        /// <value>The InputTime.</value>
        [Property("INPUT_TIME")]
        public DateTime? InputTime
        {
            get
            {
                return _InputTime;
            }
            set
            {
                _InputTime = value;
            }
        }


        /// <summary>
        /// Gets or sets the ModifyTime.()
        /// </summary>
        /// <value>The ModifyTime.</value>
        [Property("MODIFY_TIME")]
        public DateTime? ModifyTime
        {
            get
            {
                return _ModifyTime;
            }
            set
            {
                _ModifyTime = value;
            }
        }


        /// <summary>
        /// Gets or sets the DeleteTime.()
        /// </summary>
        /// <value>The DeleteTime.</value>
        [Property("DELETE_TIME")]
        public DateTime? DeleteTime
        {
            get
            {
                return _DeleteTime;
            }
            set
            {
                _DeleteTime = value;
            }
        }

        protected override void OnDelete()
        {
            MKtKeywNewstrawordHistory mHistory = new MKtKeywNewstrawordHistory();
            Common.ModelCommon.CopyObjectValues(this, mHistory);
            mHistory.DeleteTime = DateTime.Now;
            mHistory.Create();
            base.OnDelete();
        }

        public static IList<MKtKeywNewstraword> SelectAll()
        {
            return (IList<MKtKeywNewstraword>)FindAll(typeof(MKtKeywNewstraword));
        }
        public static MKtKeywNewstraword SelectByPrimaryKey(object ID)
        {
            return (MKtKeywNewstraword)FindByPrimaryKey(typeof(MKtKeywNewstraword), ID);
        }
        public static IList<MKtKeywNewstraword> SelectALLByProperty(string property, object value)
        {
            return (IList<MKtKeywNewstraword>)FindAllByProperty(typeof(MKtKeywNewstraword), property, value);
        }
        public static IList<MKtKeywNewstraword> SelectALLSliced(int firstResult, int maxResult, NHibernate.Expression.DetachedCriteria criteria)
        {

            return (IList<MKtKeywNewstraword>)SlicedFindAll(typeof(MKtKeywNewstraword), firstResult, maxResult, criteria);
        }
        
        /// <summary>
        /// 根据输入字符串（拼音助记码or代码or名称）选择向前匹配的前10条数据
        /// </summary>
        /// <param name="spell"></param>
        /// <returns></returns>
        public static MKtKeywNewstraword[] SelectByInput(string strInput)
        {
            MKtKeywNewstraword starword = new MKtKeywNewstraword();   
            string strhql = @" From MKtKeywNewstraword starword where ";
            strhql += " (starword.WordNO like '"+strInput+"%'";
            strhql += " or starword.WordName like '%" + strInput + "%'";
            strhql += " or starword.ChSpell like '"+strInput+"%')";
            strhql += " and starword.IsValidate=1 and starword.IsLeaf=1";
            SimpleQuery sq = new SimpleQuery(typeof(MKtKeywNewstraword), strhql);
            sq.SetQueryRange(10);
            return (MKtKeywNewstraword[])ExecuteQuery(sq);
        }
        /// <summary>
        /// 验证关键词是否有效（有效条件：IsValidate=1,IS_LEAF=1）
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static bool Validate(int keyword)
        {
            string hql = @"select count(*) from MKtKeywNewstraword starword where ";
            hql += " starword.WordNO=" + keyword + " and starword.IsValidate=1 and starword.IsLeaf=1";
            ScalarQuery query = new ScalarQuery(typeof(MKtKeywNewstraword),hql);
            int resultcount = Convert.ToInt32(ExecuteQuery(query));
            if (resultcount>0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
